<!--- Parameters --->
<cfparam name="s" type="string" default="" />
<cfparam name="doIndex" type="boolean" default="false" />


<!--- Function that creates and indexes the collection with games --->
<cffunction name="buildPopulateCol" output="false" returntype="void">
	<cftry>
		<cfcollection action="create" collection="igdb_games" path="#expandPath('.')#\collections" />
		<cfcatch type="any"></cfcatch>
	</cftry>
	<cfquery name="qIndex" datasource="sixdegreesimprov">
		SELECT g.game_id, g.game_name, g.descrip
		FROM igdb_games g
		ORDER BY g.game_name
	</cfquery>
	<cfindex collection="igdb_games" action="update" type="custom" query="qIndex" key="game_id" body="game_name,descrip" custom1="game_name" custom2="descrip" />
</cffunction>


<!--- Make a way to manually trigger the build and index for collection --->
<cfif (doIndex)>
	<cfset buildPopulateCol() />
</cfif>


<!--- This is slick. If collection doesn't exist, build it and THEN search. --->
<cftry>
	<cfsearch collection="igdb_games" name="qGames" criteria="#s#" maxrows="100" contextbytes="4096" contextpassages="1" contexthighlightend="</span>" contexthighlightbegin="<span class='searchTerm'>" />
	<cfcatch type="SearchEngine">
		<cfset buildPopulateCol() />
		<cfsearch collection="igdb_games" name="qGames" criteria="#s#" maxrows="100" contextbytes="4096" contextpassages="1" contexthighlightend="</span>" contexthighlightbegin="<span class='searchTerm'>" />
	</cfcatch>
</cftry>


<style type="text/css">
	span.searchTerm { color: #000000; background-color: yellow; }
</style>
<div class="post">
	<h1 class="title"><a href="#">Looking for a game?  Here's what we found.</a></h1>
	<p class="byline"><small>I spy with my little eye...</small></p>
	<div class="entry">
		<cfoutput>
			<p><strong>Your search for &quot;#s#&quot; found #qGames.recordCount# games</strong></p>
			<cfloop query="qGames">
				<h3>#custom1#</h3>
				<blockquote>
					<p>#context#</p>
					<!--- Not showing full descrip because context bytes set high enough to capture whole description
						<p>#custom2#</p>
					--->
				</blockquote>
			</cfloop>
		</cfoutput>
	</div>
</div>